<?php
/**
 * Created by PhpStorm.
 * User: xloong
 * Date: 2017/7/26
 * Time: 13:06
 */

namespace App\Http\Controllers\Api\V1;


use App\Models\YzToken;
use Dingo\Api\Http\Request;

class YouZanController extends BaseController
{
    const VIDEO_COVER = "http://annex.aitourer.com/images/794093109cd07cfebc41cdb6edd715c5.jpeg";

    public function publicLogin(Request $request){
        $youZanConfig = config('tools.youZan.public');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $youZanConfig['url']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            ['client_id'=>$youZanConfig['client_id'],'client_secret'=>$youZanConfig['client_secret']]);
        $output = curl_exec($ch);
        curl_close($ch);
        $initInfo = json_decode($output);
       if(0 == $initInfo->code){
           return $this->response->array(['code'=>200,'message'=>'ok','data'=>$initInfo->data]);
       }else{
           return $this->response->array(['code'=>20801,'message'=>'初始化商城失败','data'=>'']);
       }
    }

    public function loginYouZanYun(){
        $user     = $this->user();
        $isUpdate = false;
        $useYzToken = YzToken::where(['user_id'=>$user->id])->first();
        $useYzToken && $isUpdate = true;
        if($useYzToken && time()-strtotime($useYzToken->created_at) < $useYzToken->expired_at){
            return $this->response->array(['code'=>200,'message'=>'ok',
                'data'=>json_decode($useYzToken->scope)]);
        }
        $youZanConfig = config('tools.youZan.login');
        $post_data = ["client_id"=>$youZanConfig['client_id'],
            'client_secret'=>$youZanConfig['client_secret'],'open_user_id'=>$user->yz_user_id,
            'telephone'=>$user->yz_mobile];
        !empty($user->nickname) && $post_data['nick_name']=$user->nickname;
        !empty($user->avatar)   && $post_data['avatar']   =$user->avatar;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $youZanConfig['url']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        $loginStatus = json_decode($output);
        if(0 == $loginStatus->code){
            if(false === $isUpdate){
                $useYzToken = new YzToken();
            }
            $useYzToken->user_id      = $user->id;
            $useYzToken->access_token = $loginStatus->data->access_token;
            $useYzToken->expired_at   = 3600;
            $useYzToken->scope        = json_encode($loginStatus->data);
            $useYzToken->save();
            return $this->response->array(['code'=>200,'message'=>'ok',
                'data'=>$loginStatus->data]);
        }
        return $this->response->array(['code'=>21202,'message'=>'登陆商城失败','data'=>$loginStatus->data]);
    }

    /**
     * 有赞云退出登陆接口
     * @return mixed
     */
    public function yzLoginOut(){
        $user = $this->user();
        $youZanConfig = config('tools.youZan.logout');
        $post_data = ["client_id"=>$youZanConfig['client_id'],
            'client_secret'=>$youZanConfig['client_secret'],'open_user_id'=>$user->yz_user_id];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $youZanConfig['url']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        $loginStatus = json_decode($output);
        if(0 == $loginStatus->code){
            YzToken::where(['user_id'=>$user->id])->update(['expired_at'=>0]);
            return $this->response->array(['code'=>200,'message'=>'退出登陆成功','data'=>'']);
        }else{
           return $this->response->array(['code'=>21203,'message'=>'退出登陆失败','data'=>'']);
        }
    }

    public function getVideoCover(){
        return $this->response->array(['code'=>21203,'message'=>'获取视频封面图成功','data'=>['url'=>self::VIDEO_COVER]]);
    }
}